#!/bin/bash

# 启动后端服务脚本

echo "启动后端服务..."

# 获取脚本所在目录的绝对路径
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# 获取项目根目录
PROJECT_ROOT="$( cd "$SCRIPT_DIR/../.." && pwd )"
# 配置文件路径
CONFIG_FILE="$PROJECT_ROOT/video2text-backend/config/server.yaml"

# 检查配置文件
if [ ! -f "$CONFIG_FILE" ]; then
    echo "错误: 未找到配置文件 $CONFIG_FILE"
    exit 1
fi

# 读取配置
eval $(python3 -c "
import yaml
with open('$CONFIG_FILE') as f:
    config = yaml.safe_load(f)['server']
print(f'HOST={config[\"host\"]}')
print(f'PORT={config[\"port\"]}')
print(f'RELOAD={str(config[\"reload\"]).lower()}')
print(f'WORKERS={config[\"workers\"]}')
")

echo "使用配置:"
echo "- 监听地址: $HOST"
echo "- 监听端口: $PORT"
echo "- 热重载: $RELOAD"
echo "- 工作进程: $WORKERS"

# 进入后端目录
cd "$PROJECT_ROOT/video2text-backend"

# 构建启动命令
CMD="uvicorn app.main:app --host $HOST --port $PORT --workers $WORKERS"

# 根据reload配置添加参数
if [ "$RELOAD" = "true" ]; then
    CMD="$CMD --reload"
fi

# 启动服务
echo "执行命令: $CMD"
$CMD

echo "等待服务启动..." 